<?php

namespace iBrand\Store\Backend\Http\Controllers;

use iBrand\Backend\Http\Controllers\Controller;
use Illuminate\Http\Request;
use iBrand\Store\Backend\Repositories\BrandRepository;
use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;
use Validator;

class BrandController extends Controller
{
	protected $brandRepository;

	public function __construct(BrandRepository $brandRepository)
	{
		$this->brandRepository = $brandRepository;
	}

	public function index()
	{
		$brand = $this->brandRepository->all();

		return LaravelAdmin::content(function (Content $content) use ($brand) {

			$content->header('品牌列表');

			$content->breadcrumb(
				['text' => '品牌管理', 'url' => 'store/brand', 'no-pjax' => 1],
				['text' => '品牌列表', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']

			);

			$content->body(view('store-backend::brand.index', compact('brand')));
		});
	}

	public function create()
	{
		return LaravelAdmin::content(function (Content $content) {

			$content->header('添加品牌');

			$content->breadcrumb(
				['text' => '品牌管理', 'url' => 'store/brand', 'no-pjax' => 1],
				['text' => '添加品牌', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']
			);

			$content->body(view('store-backend::brand.create'));
		});
	}

	/**
	 * @param \Illuminate\Http\Request $request
	 *
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function store(Request $request)
	{
		$input   = $request->except('id', '_token', 'file');
		$rules   = [
			'name' => "required",
			'url'  => "required",
			'logo' => "required",
			'sort' => "required|integer",
		];
		$message = [
			"required" => ":attribute 不能为空",
			"integer"  => ":attribute 必须为整数",
		];

		$attributes = [
			'name' => "品牌名称",
			'url'  => "品牌网址",
			'logo' => "品牌LOGO",
			'sort' => "排序",
		];
		$validator  = Validator::make($input, $rules, $message, $attributes);
		if ($validator->fails()) {
			$warnings = $validator->messages();
			$warning  = $warnings->first();

			return $this->ajaxJson(false, [], 500, $warning);
		}

		try {
			if (request('id')) {
				$this->brandRepository->update($input, request('id'));
			} else {
				$this->brandRepository->create($input);
			}

			return $this->ajaxJson();
		} catch (\Exception $exception) {
			\Log::info($exception->getTraceAsString());

			return $this->ajaxJson(false, [], 500, $exception->getMessage());
		}
	}

	public function edit($id)
	{
		$brand_list = $this->brandRepository->findByField('id', $id)->first();

		return LaravelAdmin::content(function (Content $content) use ($brand_list) {

			$content->header('编辑品牌');

			$content->breadcrumb(
				['text' => '品牌管理', 'url' => 'store/brand', 'no-pjax' => 1],
				['text' => '编辑品牌', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']

			);

			$content->body(view('store-backend::brand.edit', compact('brand_list')));
		});
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int $id
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{
		$this->brandRepository->delete($id);

		return redirect()->back()->withFlashSuccess('品牌已删除');
	}
}
